package com.chencq.core.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 拦截器
 * 
 * @comment:
 * @author chencq
 * @Email:hero901027@163.com
 */
public class MyInterceptor implements HandlerInterceptor {

	protected Logger logger = LoggerFactory.getLogger(MyInterceptor.class);

	/**
	 * 执行时间：视图已经解析完毕，类似try catch之后的finally
	 */
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e) throws Exception {
		logger.info("MyInterceptor  afterCompletion ......");
	}

	/**
	 * 执行时间：controller执行完，视图解析器还没有把视图解析成页面，对视图做统一的修改，主要体现在model上
	 */
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView model) throws Exception {
		logger.info("MyInterceptor postHandle.....");
	}

	/**
	 * 执行时机：在controller之前执行，返回类型为boolean，true代表放行访问controller，
	 * false表示不可以访问controller
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		logger.info("MyInterceptor preHandle ......" + request.getRequestURI());
		return true;
	}

}
